package com.whut.rssReader.activity;

import java.util.Date;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import com.whut.rssReader.db.RssInfo;
import com.whut.rssReader.entity.Resource;

public class ResAddDialog extends MyDialog {
	
	public EditText et_resource_name;
	public EditText et_resource_link;
	public Spinner spinner_sort;
	public Button bt_add_resource;
	
	private Resource resourceView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		resourceView = (Resource) intent.getSerializableExtra("resourceView");
		findView();
	}
	
	@Override
	public View getPageView() {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		View pageView = layoutInflater.inflate(R.layout.add_resource_dialog,null);
		return pageView;
	}
	
	private void findView(){
		et_resource_name = (EditText) findViewById(R.id.add_resource_name);
		et_resource_link = (EditText) findViewById(R.id.add_resource_link);
		spinner_sort = (Spinner) findViewById(R.id.spinner_sort);
		final Cursor c = contentResolver.query(RssInfo.CONTENT_URI_SORT,null, null, null, null);
		SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, 
				c, new String[]{RssInfo.SORT_NAME}, new int[]{android.R.id.text1});
		sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_sort.setAdapter(sca);
		bt_add_resource = (Button)findViewById(R.id.bt_add_resource);
	    bt_add_resource.setOnClickListener(this);
		findViewById(R.id.bt_cancle_resource).setOnClickListener(this);
		if(resourceView != null){
			et_resource_name.setText(resourceView.name);
			et_resource_link.setText(resourceView.link);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.bt_add_resource:
			if(TextUtils.isEmpty(et_resource_name.getText())){
				et_resource_name.setError("Please enter a resource");
				return;
			}
			if(TextUtils.isEmpty(et_resource_link.getText())){
				et_resource_link.setError("Please enter a link");
				return;
			}
			Cursor c = (Cursor) spinner_sort.getSelectedItem();
			String sort_name = c.getString(c.getColumnIndex(RssInfo.SORT_NAME));
			c.close();
			
			ContentValues contentValue = new ContentValues();
			contentValue.put(RssInfo.RESOURCE_NAME, "	" + et_resource_name.getText().toString());
			contentValue.put(RssInfo.RESOURCE_LINK, et_resource_link.getText().toString());
			contentValue.put(RssInfo.RESOURCE_SORT,sort_name);
			contentValue.put(RssInfo.RESOURCE_DATE, new Date().toString());
			if(resourceView != null){
				Cursor resCursor = managedQuery(RssInfo.CONTENT_URI_RESOURCE, null, RssInfo.RESOURCE_ID + "=" + resourceView.id, null, null);
				if(resCursor==null || resCursor.getCount() <=0){
					contentResolver.insert(RssInfo.CONTENT_URI_RESOURCE, contentValue);

				}else{
					contentResolver.update(RssInfo.CONTENT_URI_RESOURCE, contentValue, RssInfo.RESOURCE_ID + "=" + resourceView.id, null);
				}
			}else{
				contentResolver.insert(RssInfo.CONTENT_URI_RESOURCE, contentValue);
			}
			this.finish();
			break;
		case R.id.bt_cancle_resource:
			this.finish();
			break;
		}
		
	}

}
